import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TwoPlayerWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TwoPlayerWorld extends World
{

    public SnakeZero snakeZero;  // 用来存储 Snake 对象
    public SnakeOne snakeOne;
    private GreenfootImage scoreImage;  // 显示得分的图片
    public TwoPlayerWorld()
    {    
        super(1200,800, 1,false); 
        addObject(new SnakeZero(this), getWidth()/3, getHeight()/2);
        addObject(new SnakeOne(this), 2*getWidth()/3,getHeight()/2);
        addObject(new Food0(), 10+Greenfoot.getRandomNumber(getWidth()-20), 10+Greenfoot.getRandomNumber(getHeight()-20));
        setPaintOrder(Snake.class);   
        Greenfoot.setSpeed(35);
        
        // 获取 Snake 对象
        snakeZero = getObjects(SnakeZero.class).get(0);
        snakeOne = getObjects(SnakeOne.class).get(0);
        
        addObject(new Button_back(), 30, 50);
        updateScore();  // 初始化时更新得分显示
    }
    // 更新得分
    public void updateScore()
    {
        // 获取 Snake 的长度，作为得分
        int scoreZero = snakeZero.getLength(); 
        int scoreOne = snakeOne.getLength();

        // 显示得分在左上角
        showText("玩家零得分: " + (scoreZero-1), 60,30);  // 这里直接使用 showText 来显示得分
        showText("玩家一得分: " + (scoreOne-1), getWidth()-80,30); 
    }
     // 每帧都更新得分
    public void act()
    {
        updateScore();
    }
}
